package com.sense.kukoo.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;


import java.io.File;
import java.util.Locale;
import java.util.UUID;

/**
 * <br> ClassName:   PhoneUtil
 * <br> Description: 手机、系统相关工具类
 * <br>
 * <br> Author:      zhangweiqiang
 * <br> Date:        2017/8/2 14:09
 */
public class PhoneUtil {
    /**
     * <br> Description: 获取操作系统
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 10:33
     *
     * @return 返回操作系统
     */
    public static String getOS() {
        return "Android";
    }

    /**
     * <br> Description: 获取系统类别
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 15:23
     *
     * @return 返回系统类别
     */
    public static String getSysNo() {
        return "TDW_APP";
    }

    /**
     * <br> Description: 获取操作系统版本
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 10:33
     *
     * @return 返回操作系统版本
     */
    public static String getOSVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * <br> Description: 获取设备语言
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 14:44
     *
     * @return 返回设备语言
     */
    public static String getDeviceLanguage() {
        Locale l = Locale.getDefault();
        return l.getLanguage();
    }

    /**
     * <br> Description: 获取手机品牌
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 14:46
     *
     * @return 返回手机品牌
     */
    public static String getBrand() {
        return Build.BRAND;
    }

    /**
     * <br> Description: 获取手机型号
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 14:47
     *
     * @return 返回手机型号
     */
    public static String getModel() {
        return Build.MODEL;
    }

    /**
     * <br> Description: 获取运营商信息
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 09:56
     *
     * @return 返回运营商信息
     */
    public static String getSimOperatorInfo() {
        TelephonyManager telephonyManager = (TelephonyManager) ApplicationContext.instance().getSystemService(Context.TELEPHONY_SERVICE);
        String operatorString = telephonyManager.getSimOperator();

        if (operatorString == null) {
            return "";
        } else {
            return operatorString;
        }
    }

    /**
     * <br> Description: 获取手机屏幕密度api
     * <br> Author:      zhangweiqiang
     * <br> Date:        2017/8/2 14:45
     *
     * @return 返回手机屏幕密度
     */
    public static float getScreenDensity() {
        DisplayMetrics dm = ApplicationContext.instance().getResources().getDisplayMetrics();
        return dm.density;
    }

    /**
     * <br> Description: 获取屏幕宽度
     * <br> Author:      longluliu
     * <br> Date:        2013/6/03 10:35
     *
     * @param context 上下文
     * @return 返回屏幕宽度
     */
    public static int getScreenPixelsWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    /**
     * <br> Description: 获取屏幕高度
     * <br> Author:      longluliu
     * <br> Date:        2013/6/03 10:35
     *
     * @param context 上下文
     * @return 返回屏幕高度
     */
    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.heightPixels;
    }
    public static Point getScreenSize(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Point realSize = new Point();
        display.getRealSize(realSize);
        return realSize;
    }
    /**
     * <br> Description: 获取屏幕分辨率
     * <br> Author:      hehaodong
     * <br> Date:        2014/12/24 14:17
     *
     * @return 返回屏幕分辨率
     */
    public static String getResolution() {
        return getScreenPixelsWidth(ApplicationContext.instance()) + "*" + getScreenHeight(ApplicationContext.instance());
    }

    /**
     * <br> Description: 获取屏幕宽高
     * <br> Author:      zhangweiqiang
     * <br> Date:        2017/7/26 9:56
     *
     * @param mContext 上下文
     * @return 返回屏幕宽高数组
     */
    public static int[] getDefaultDisplay(Context mContext) {
        int[] size = new int[]{0, 0};
        if (mContext != null) {
            WindowManager wm = (WindowManager) mContext
                    .getSystemService(Context.WINDOW_SERVICE);
            size = new int[]{wm.getDefaultDisplay().getWidth(), wm.getDefaultDisplay().getHeight()};
        }
        return size;
    }


    /**
     * 获取手机设备名
     *
     * @return
     */
    public static String getDeviceModel() {
        return Build.MODEL;
    }

    /**
     * 获取系统sdk版本号
     *
     * @return
     */
    public static String getSystemSdkVersion() {
        return Build.VERSION.RELEASE;
    }


    /**
     * 获取AndroidId
     *
     * @param context
     * @return
     */
    public static String getAndroidId(Context context) {
        return Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); // 获取AndroidId
    }

    /**
     * 获取wifi物理地址
     *
     * @param context
     * @return
     */
    public static String getWifiMac(Context context) {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        String wifiMac = info.getMacAddress();
        return wifiMac;
    }

    /**
     * 获取设备的唯一标识
     *
     * @param context
     * @return 唯一标识字符串
     */
    public static String getDeviceID(Context context) {
        String uuid = "";

        if (TextUtils.isEmpty(uuid)) {
            uuid = getAndroidId(context);
        }
        if (TextUtils.isEmpty(uuid)) {
            uuid = autoGenerateUUID(context);
        }

        return uuid;
    }

    /**
     * 获取一个uuid
     *
     * @param context
     * @return
     */
    public static String autoGenerateUUID(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("cacheMap", Context.MODE_PRIVATE);
        String uuid = preferences.getString("uuid", "");
        if (TextUtils.isEmpty(uuid)) {
            uuid = UUID.randomUUID().toString();
            preferences.edit().putString("uuid", uuid).apply();
        }
        return uuid;
    }

    /**
     * @Author Lionszhang
     * @Date 2021/5/11 20:51
     * @Instruction 获取手机内部+外部总可利用存储空间大小
     */
    public static String getPhoneAvailableRoom() {
        return getPhoneSystemAvailableRoom() + getPhoneExternalAvailableRoom() + "G";

    }

    /**
     * @Author Lionszhang
     * @Date 2021/5/11 20:51
     * @Instruction 获取手机内部+外部总存储空间大小  手机存储总大小
     */
    public static String getPhoneRoom() {
        return getPhoneExternalRoom() + getPhoneSystemRoom() + "G";
    }
    /**
     * @Author Lionszhang
     * @Date 2021/5/11 20:51
     * @Instruction  获取手机已用存储大小
     */
    public static String getPhoneUsedRoom() {
        return (getPhoneExternalRoom() + getPhoneSystemRoom())-(getPhoneSystemAvailableRoom() + getPhoneExternalAvailableRoom())+"G" ;
    }





    /**
     * @Author Lionszhang
     * @Date 2021/5/11 20:51
     * @Instruction 获取手机内部可用存储空间大小
     */
    public static long getPhoneSystemAvailableRoom() {
        File root = Environment.getRootDirectory();
        StatFs sf = new StatFs(root.getPath());
        long blockSize = sf.getBlockSizeLong();
        long blockCount = sf.getBlockCountLong();
        long availCount = sf.getAvailableBlocksLong();
        Log.d("", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:" + blockSize * blockCount / 1024 + "KB");
        Log.d("", "可用的block数目：:" + availCount + ",可用大小:" + availCount * blockSize / 1024 + "KB");
        return availCount * blockSize / (1024*1024*1024);//单位G
    }

    /**
     * @Author Lionszhang
     * @Date 2021/5/11 20:51
     * @Instruction 获取手机内部存储空间大小
     */
    public static long getPhoneSystemRoom() {
        File root = Environment.getRootDirectory();
        StatFs sf = new StatFs(root.getPath());
        long blockSize = sf.getBlockSizeLong();
        long blockCount = sf.getBlockCountLong();
        long availCount = sf.getAvailableBlocksLong();
        Log.d("", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:" + blockSize * blockCount / 1024 + "KB");
        Log.d("", "可用的block数目：:" + availCount + ",可用大小:" + availCount * blockSize / 1024 + "KB");
        return blockSize * blockCount / (1024*1024*1024);//单位G

    }

    /**
     * @Author Lionszhang
     * @Date 2021/5/11 20:51
     * @Instruction 获取手机外部可利用存储空间大小
     */
    public static long getPhoneExternalAvailableRoom() {
        long blockSize;
        long blockCount;
        long availCount;
        StatFs stat;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File path = Environment.getExternalStorageDirectory();
            stat = new StatFs(path.toString());
            //之后的有些版本有了新的方法进行替代。
            blockSize = stat.getBlockSizeLong();
            blockCount = stat.getBlockCountLong();
            availCount = stat.getAvailableBlocksLong();
            Log.d("", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:" + blockSize * blockCount / 1024 + "KB");
            Log.d("", "可用的block数目：:" + availCount + ",剩余空间:" + availCount * blockSize / 1024 + "KB");
            return availCount * blockSize / (1024*1024*1024);//单位G
        }
        return 0;
    }

    /**
     * @Author Lionszhang
     * @Date 2021/5/11 20:51
     * @Instruction 获取手机外部存储空间大小
     */
    public static long getPhoneExternalRoom() {
        long blockSize;
        long blockCount;
        long availCount;
        StatFs stat;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File path = Environment.getExternalStorageDirectory();
            stat = new StatFs(path.toString());
            //之后的有些版本有了新的方法进行替代。
            blockSize = stat.getBlockSizeLong();
            blockCount = stat.getBlockCountLong();
            availCount = stat.getAvailableBlocksLong();
            Log.d("", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:" + blockSize * blockCount / 1024 + "KB");
            Log.d("", "可用的block数目：:" + availCount + ",剩余空间:" + availCount * blockSize / 1024 + "KB");
            return blockSize * blockCount / (1024*1024*1024);//单位G
        }
        return 0;
    }
}
